<?php

class Category extends CI_Model
{

    var $id = '';
    var $name = '';

    function __construct()
    {
        parent::__construct();
    }

    function get_all()
    {
        $query = $this->db->get('Category');
        return $query->result();
    }

    function get_last_ten_entries()
    {
        $query = $this->db->get('Category', 10);
        return $query->result();
    }

    function save()
    {
        try {
            $this->name = $this->input->post('name');
            if ($this->name)
                $this->db->insert('Category', $this);
            else return 0;
        } catch (Exception $e) {
            return 0;
        }
        return 1;
    }

    function update($id, $data)
    {
        try {
            $this->db->where("id", $id);
            $this->db->update('Category', $data);
        } catch (Exception $e) {
            return 0;
        }
        return 1;
    }

    function delete($id)
    {

        $this->db->where("id", $id);
        $this->db->delete("category");
        if ($this->db->_error_number() == 1451) {
            return 0;
        } else return 1;
    }

}